home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 2 / Amiga Tools 2.iso / mui / mui-tools / multiuser / src / support / mlist.c < prev    next >
C/C++ Source or Header  |  1995-03-09  |  7KB  |  261 lines

  1. /************************************************************
  2. * MultiUser - MultiUser Task/File Support System                *
  3. * ---------------------------------------------------------    *
  4. * List Clone                                                                *
  5. * ---------------------------------------------------------    *
  6. * © Copyright 1993-1994 Geert Uytterhoeven                        *
  7. * All Rights Reserved.                                                    *
  8. ************************************************************/
  9.  
  10.  
  11. #include <exec/types.h>
  12. #include <exec/memory.h>
  13. #include <dos/dos.h>
  14. #include <dos/dosasl.h>
  15. #include <proto/exec.h>
  16. #include <proto/dos.h>
  17. #include <string.h>
  18. #include <libraries/multiuser.h>
  19. #include <proto/multiuser.h>
  20.  
  21. #include "MList_rev.h"
  22.  
  23. #include "Locale.h"
  24.  
  25. char __VersTag__[] = VERSTAG;
  26.  
  27.  
  28. static void __regargs DumpInfo(struct FileInfoBlock *fib,
  29.                                          struct muUserInfo *uinfo,
  30.                                          struct muGroupInfo *ginfo, BOOL groups,
  31.                                          struct ExecBase *SysBase,
  32.                                          struct DosLibrary *DOSBase,
  33.                                          struct muBase *muBase);
  34.  
  35.  
  36.     /*
  37.      *        This table makes it easier to set the Protection Flags the right way
  38.      */
  39.  
  40. struct ProtectionEntry {
  41.     ULONG Bit;
  42.     ULONG ActiveLow;        /* NULL, or the same as Bit above */
  43. };
  44.  
  45.  
  46. int __saveds Start(char *arg)
  47. {
  48.     struct ExecBase *SysBase;
  49.     struct DosLibrary *DOSBase;
  50.     struct muBase *muBase = NULL;
  51.     struct RDArgs *rdargs;
  52.     struct muUserInfo *uinfo;
  53.     struct muGroupInfo *ginfo;
  54.     LONG argarray[] = {
  55. #define argDIR        0
  56. #define argGROUPS    1
  57.         NULL, NULL
  58.     };
  59.     struct AnchorPath *anchor;
  60.     ULONG files, dirs, blocks;
  61.     ULONG tfiles = 0, tdirs = 0, tblocks = 0;
  62.     LONG args[3];
  63.     char **name;
  64.     static char *defaultname[] = {
  65.         "", NULL
  66.     };
  67.     int numdirs = 0;
  68.     LONG error;
  69.     int rc = RETURN_OK;
  70.     struct LocaleInfo li;
  71.  
  72.     SysBase = *(struct ExecBase **)4;
  73.  
  74.     if ((!(DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 37))) ||
  75.          (!(muBase = (struct muBase *)OpenLibrary("multiuser.library", 39)))) {
  76.         rc = ERROR_INVALID_RESIDENT_LIBRARY;
  77.         goto Exit;
  78.     }
  79.  
  80.     OpenLoc(&li);
  81.  
  82.     rdargs = ReadArgs("DIR/M,GROUPS/S", argarray, NULL);
  83.     if (!rdargs)
  84.         error = IoErr();
  85.     else
  86.         if (uinfo = muAllocUserInfo()) {
  87.             if (ginfo = muAllocGroupInfo()) {
  88.                 if (anchor = (struct AnchorPath *)AllocVec(sizeof(struct AnchorPath),
  89.                                                                          MEMF_CLEAR)) {
  90.                     anchor->ap_BreakBits = SIGBREAKF_CTRL_C;
  91.                     anchor->ap_Flags = APF_DOWILD;
  92.                     if (argarray[argDIR])
  93.                         name = (char **)argarray[argDIR];
  94.                     else
  95.                         name = defaultname;
  96.                     do {
  97.                         files = dirs = blocks = 0;
  98.                         if (!(error = MatchFirst(name[0], anchor))) {
  99.                             if (IsFileSystem(name[0]))
  100.                                 if (!(anchor->ap_Flags & APF_ITSWILD) &&
  101.                                      (anchor->ap_Info.fib_DirEntryType > 0)) {
  102.                                     if (strlen(name[0])) {
  103.                                         args[0] = (LONG)name[0];
  104.                                         VPrintf(GetLocS(&li,MSG_DIRECTORY), args);
  105.                                     }
  106.                                     anchor->ap_Flags |= APF_DODIR;
  107.                                     error = MatchNext(anchor);
  108.                                 } else {}
  109.                             else                    
  110.                                 error = ERROR_OBJECT_WRONG_TYPE;
  111.                             if (!error)
  112.                                 do
  113.                                     if (anchor->ap_Flags & APF_DIDDIR)
  114.                                         anchor->ap_Flags &= ~APF_DIDDIR;
  115.                                     else {
  116.                                         DumpInfo(&anchor->ap_Info, uinfo, ginfo,
  117.                                                     argarray[argGROUPS], SysBase, DOSBase,
  118.                                                     muBase);
  119.                                         if (anchor->ap_Info.fib_DirEntryType < 0)
  120.                                             files++;
  121.                                         else
  122.                                             dirs++;
  123.                                         blocks += anchor->ap_Info.fib_NumBlocks+1;
  124.                                     }
  125.                                 while (!(error = MatchNext(anchor)));
  126.                             if (error == ERROR_NO_MORE_ENTRIES)
  127.                                 error = NULL;
  128.                         } else if (error == ERROR_NO_MORE_ENTRIES)
  129.                             error = ERROR_OBJECT_NOT_FOUND;
  130.                         if (!error) {
  131.                             args[0] = files;
  132.                             args[1] = dirs;
  133.                             args[2] = blocks;
  134.                             VPrintf(GetLocS(&li,MSG_USAGE), args);
  135.                             tfiles += files;
  136.                             tdirs += dirs;
  137.                             tblocks += blocks;
  138.                             numdirs++;
  139.                         }
  140.                         MatchEnd(anchor);
  141.                     } while (!error && *(name = &name[1]));
  142.                     if (!error && (numdirs > 1)) {
  143.                         PutStr(GetLocS(&li,MSG_TOTAL));
  144.                         PutStr(" ");
  145.                         args[0] = tfiles;
  146.                         args[1] = tdirs;
  147.                         args[2] = tblocks;
  148.                         VPrintf(GetLocS(&li,MSG_USAGE), args);
  149.                     }
  150.                     FreeVec(anchor);
  151.                 } else
  152.                     error = IoErr();
  153.                 muFreeGroupInfo(ginfo);
  154.             } else
  155.                 error = IoErr();
  156.             muFreeUserInfo(uinfo);
  157.         } else
  158.             error = IoErr();
  159.     FreeArgs(rdargs);
  160.     if (error) {
  161.         PrintFault(error, NULL);
  162.         rc = RETURN_ERROR;
  163.     }
  164.  
  165.     CloseLoc(&li);
  166.  
  167. Exit:
  168.     CloseLibrary((struct Library *)muBase);
  169.     CloseLibrary((struct Library *)DOSBase);
  170.  
  171.     return(rc);
  172. }    
  173.  
  174.  
  175.     /*
  176.      *        Dump the Information for a Directory Entry
  177.      */
  178.  
  179. static void __regargs DumpInfo(struct FileInfoBlock *fib,
  180.                                          struct muUserInfo *uinfo,
  181.                                          struct muGroupInfo *ginfo, BOOL groups,
  182.                                          struct ExecBase *SysBase,
  183.                                          struct DosLibrary *DOSBase,
  184.                                          struct muBase *muBase)
  185. {
  186.     ULONG stream[6];
  187.     char protection[17];
  188.     char *fmt;
  189.     int i = 0;
  190.     struct DateTime dt;
  191.     char date[LEN_DATSTRING];
  192.  
  193.     static struct ProtectionEntry pe[] = {
  194.         muFIBF_SET_UID, FALSE,
  195.         FIBF_SCRIPT, FALSE,
  196.         FIBF_PURE, FALSE,
  197.         FIBF_ARCHIVE, FALSE,
  198.         FIBF_READ, FIBF_READ,
  199.         FIBF_WRITE, FIBF_WRITE,
  200.         FIBF_EXECUTE, FIBF_EXECUTE,
  201.         FIBF_DELETE, FIBF_DELETE,
  202.         FIBF_GRP_READ, FALSE,
  203.         FIBF_GRP_WRITE, FALSE,
  204.         FIBF_GRP_EXECUTE, FALSE,
  205.         FIBF_GRP_DELETE, FALSE,
  206.         FIBF_OTR_READ, FALSE,
  207.         FIBF_OTR_WRITE, FALSE,
  208.         FIBF_OTR_EXECUTE, FALSE,
  209.         FIBF_OTR_DELETE, FALSE,
  210. };
  211.     static char ProtectionTemplate[] = "usparwedrwedrwed";
  212.  
  213.     stream[i++] = (ULONG)&fib->fib_FileName;
  214.     if (fib->fib_DirEntryType < 0)
  215.         stream[i++] = fib->fib_Size;
  216.     dt.dat_Stamp = fib->fib_Date;
  217.     dt.dat_Format = FORMAT_DOS;
  218.     dt.dat_Flags = NULL;
  219.     dt.dat_StrDay = NULL;
  220.     dt.dat_StrDate = date;
  221.     dt.dat_StrTime = NULL;
  222.     DateToStr(&dt);
  223.     stream[i++] = (ULONG)protection;
  224.     stream[i++] = (ULONG)date;
  225.     if (fib->fib_OwnerUID == muNOBODY_UID) {
  226.         strcpy(uinfo->UserID, "\b");
  227.         groups = FALSE;
  228.     } else {
  229.         uinfo->uid = fib->fib_OwnerUID;
  230.         if (!muGetUserInfo(uinfo, muKeyType_uid))
  231.             strcpy(uinfo->UserID, "???");
  232.     }
  233.     stream[i++] = (LONG)&uinfo->UserID;
  234.  
  235.     if (groups) {
  236.         ginfo->gid = fib->fib_OwnerGID;
  237.         if (!muGetGroupInfo(ginfo, muKeyType_gid))
  238.             strcpy(ginfo->GroupID, "???");
  239.         stream[i] = (LONG)&ginfo->GroupID;
  240.     }
  241.  
  242.     for (i = 0; i < 16; i++)
  243.         if ((fib->fib_Protection & pe[i].Bit) ^ pe[i].ActiveLow)
  244.             protection[i] = ProtectionTemplate[i];
  245.         else
  246.             protection[i] = '-';
  247.     protection[i] = '\0';
  248.  
  249.     if (fib->fib_DirEntryType < 0)
  250.         if (groups)
  251.             fmt = "%-30s %8ld %s %s %s [%s]\n";
  252.         else
  253.             fmt = "%-30s %8ld %s %s %s\n";
  254.     else
  255.         if (groups)
  256.             fmt = "%-30s      Dir %s %s %s [%s]\n";
  257.         else
  258.             fmt = "%-30s      Dir %s %s %s\n";
  259.     VPrintf(fmt, stream);
  260. }
  261.